package org.mspring.mlog.service;

import java.util.List;

import org.mspring.mlog.dao.LinkDao;
import org.mspring.mlog.entity.Link;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class LinkService {

    @Autowired
    private LinkDao linkDao;

    public Link get(long id) {
        return linkDao.get(id);
    }

    public Page<Link> listPage(QueryBuilder queryBuilder, Page<Link> page) {
        return linkDao.listPage(queryBuilder, page);
    }

    public List<Link> listVisableLinks() {
        return linkDao.listVisableLinks();
    }

    public List<Link> listVisableLinks(Long linkType) {
        return linkDao.listVisableLinks(linkType);
    }

    public void delete(Long... ids) {
        linkDao.delete(ids);
    }

    public void update(Link link) {
        linkDao.update(link);
    }

    public void updateLinkVisable(boolean visable, Long[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (Long id : ids) {
            linkDao.updateLinkVisable(id, visable);
        }
    }

    public void updateLinkOrders(Long[] ids, Long[] orders) {
        if (ids == null || orders == null || ids.length != orders.length || ids.length == 0) {
            return;
        }
        for (int i = 0; i < orders.length; i++) {
            long id = ids[i];
            long order = orders[i];
            linkDao.updateLinkSort(id, order);
        }
    }

    public Long insert(Link link) {
        link.setVisable(true);
        return linkDao.insert(link);
    }

}
